package elitech.vietnam.weatherfashion.ultilities;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.backgroundtask.NotificationBackGroundService;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.dialog.ConfirmDialog;

@SuppressLint("SimpleDateFormat")
public abstract class Utilities {

	public static int getDrawableFromTypeOfWeather(Context context, String type) {

		if (type == null || type.equals(""))
			return R.drawable.ic_launcher;

		type = type.trim().toLowerCase();
		String[] listType = null;

		// trong veo
		listType = context.getResources().getStringArray(R.array.trong);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_trong;
		}

		// am_u
		listType = context.getResources().getStringArray(R.array.am_u);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_am_u;
		}

		// mua
		listType = context.getResources().getStringArray(R.array.mua);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_mua;
		}

		// mua_giong
		listType = context.getResources().getStringArray(R.array.mua_giong);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_mua_giong;
		}

		// mua_rao
		listType = context.getResources().getStringArray(R.array.mua_rao);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_mua_rao;
		}

		// mua_tuyet
		listType = context.getResources().getStringArray(R.array.mua_tuyet);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_mua_tuyet;
		}

		// nhieu_may
		listType = context.getResources().getStringArray(R.array.nhieu_may);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_nhieu_may;
		}

		// suong_mu
		listType = context.getResources().getStringArray(R.array.suong_mu);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_suong_mu;
		}

		// tuyet_bat_chot
		listType = context.getResources().getStringArray(R.array.tuyet_bat_chot);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_tuyet_bat_chot;
		}

		// rain
		if (type.contains("rain"))
			return R.drawable.icon_mua;

		// rain
		if (type.contains("drizzle"))
			return R.drawable.icon_mua;

		// snow
		if (type.contains("snow"))
			return R.drawable.icon_tuyet;

		return R.drawable.ic_launcher;
	}

	public static int getTabIconDrawableFromTypeOfWeather(Context context, String type) {

		if (type == null || type.equals(""))
			return R.drawable.tab_icon_trong;

		type = type.trim().toLowerCase();
		String[] listType = null;

		// trong veo
		listType = context.getResources().getStringArray(R.array.trong);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_trong;
		}

		// am_u
		listType = context.getResources().getStringArray(R.array.am_u);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_am_u;
		}

		// mua
		listType = context.getResources().getStringArray(R.array.mua);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_mua;
		}

		// mua_giong
		listType = context.getResources().getStringArray(R.array.mua_giong);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_mua_giong;
		}

		// mua_rao
		listType = context.getResources().getStringArray(R.array.mua_rao);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_mua_rao;
		}

		// mua_tuyet
		listType = context.getResources().getStringArray(R.array.mua_tuyet);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_mua_tuyet;
		}

		// nhieu_may
		listType = context.getResources().getStringArray(R.array.nhieu_may);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_nhieu_may;
		}

		// suong_mu
		listType = context.getResources().getStringArray(R.array.suong_mu);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.tab_icon_suong_mu;
		}

		// tuyet_bat_chot
		listType = context.getResources().getStringArray(R.array.tuyet_bat_chot);
		for (String string : listType) {
			if (string.trim().contains(type))
				return R.drawable.icon_tuyet_bat_chot;
		}

		// rain
		if (type.contains("rain"))
			return R.drawable.tab_icon_mua;

		// rain
		if (type.contains("drizzle"))
			return R.drawable.tab_icon_mua;

		// snow
		if (type.contains("snow"))
			return R.drawable.tab_icon_tuyet;

		return R.drawable.tab_icon_trong;
	}

	public static String convertDateFormat(String date, String oldFormat, String newFormat) {
		try {
			Date d = new SimpleDateFormat(oldFormat, Locale.US).parse(date);
			DateFormat df = new SimpleDateFormat(newFormat, Locale.US);
			return df.format(d);
		} catch (Exception e) {
			e.printStackTrace();
			return date;
		}
	}

	public static Date parseDateFromStringFormat(String date, String format) {
		try {
			Date d = new SimpleDateFormat(format/* , Locale.US */).parse(date);
			return d;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/*
	 * example dateStringFromJson "/Date(1383843600000)/"
	 */
	public static Date parseDateFromJson(String dateStringFromJson) {

		try {
			if (dateStringFromJson == null || dateStringFromJson.equals("null") || dateStringFromJson.equals(""))
				return null;
			else {
				int startIndex = dateStringFromJson.indexOf('(') + 1;
				int endIndex = dateStringFromJson.indexOf(')');
				String milisecondString = dateStringFromJson.substring(startIndex, endIndex);
				return new Date(Long.parseLong(milisecondString));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void checkNetworkState(final Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

		boolean isOnline = cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();

		if (!isOnline) {
			try {
				new ConfirmDialog.Builder(context).setMessage("Cannot access internet, Please change your wifi setting.")
						.setYesClick(new ConfirmDialog.OnYesClick() {
							@Override
							public void doClick() {
								context.startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
							}
						}).build().show();
			} catch (Exception e) {
				e.printStackTrace();
				Toast.makeText(context, "Access network error: " + e, Toast.LENGTH_SHORT).show();
			}
		}
	}

	public static void checkGpsState(final Context context) {
		final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

		if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
			Toast.makeText(context, "GPS is disable!", Toast.LENGTH_SHORT).show();
			try {

				context.startActivity(new Intent("android.settings.LOCATION_SOURCE_SETTINGS"));
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

	public static void changeConfigLanguage(Context context, String code) {
		try {
			Resources res = context.getResources();
			DisplayMetrics dm = res.getDisplayMetrics();
			android.content.res.Configuration conf = res.getConfiguration();
			conf.locale = new Locale(code);
			res.updateConfiguration(conf, dm);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getPublicIpAddress() {
		String ip = "";
		Utils.Log("Start get ip ");
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
			// HttpGet httpget = new HttpGet("http://whatismyip.com.au/");
			// HttpGet httpget = new HttpGet("http://www.whatismyip.org/");
			HttpResponse response;

			response = httpclient.execute(httpget);
			// Log.i("externalip",response.getStatusLine().toString());

			HttpEntity entity = response.getEntity();
			entity.getContentLength();
			String str = EntityUtils.toString(entity);

			JSONObject json_data = new JSONObject(str);
			ip = json_data.getString("ip");

		} catch (Exception e) {
			e.printStackTrace();
			Log.e("DEBUG", "getPublicIpAddress error! " + e);
		}

		Utils.Log("Stop public ip: " + ip);
		return ip;
	}

	public static void startNotificationService(Context context) {
		try {
			Intent intent = new Intent(context, NotificationBackGroundService.class);
			context.startService(intent);
		} catch (Exception e) {
			Log.e("DEBUG", "startNotificationService error! " + e);
		}
	}

	public static void stopNotificationService(Context context) {
		try {
			Intent intent = new Intent(context, NotificationBackGroundService.class);
			context.stopService(intent);
		} catch (Exception e) {
			Log.e("DEBUG", "stopNotificationService error! " + e);
		}
	}

	public static String convertCountryCodeToLanguageCode(String countryCode) {

		if (countryCode.equals("VN")) {
			return "vi";
		}
		if (countryCode.equals("US")) {
			return "en";
		}
		if (countryCode.equals("KR")) {
			return "ko";
		}
		if (countryCode.equals("CN")) {
			return "zh";
		}
		return "en";
	}

}
